// home.js
const { songs } = require('../../data/songs.js');

Page({
  data: {
    guessSongs: [], // 猜你喜欢
    popularSongs: [], // 大家都在听
    likedSongs: [], // 我的喜欢
    playlists: [
      { id: 1, name: '午后花园', cover: '/assets/playlists/1.png' },
      { id: 2, name: '深夜咖啡', cover: '/assets/playlists/2.png' },
      { id: 3, name: '雨天爵士', cover: '/assets/playlists/3.png' }
    ]
  },

  onLoad(options) {
    // 随机选择3首歌作为猜你喜欢
    const shuffled = [...songs].sort(() => 0.5 - Math.random());
    const guessSongs = shuffled.slice(0, 3);
    
    // 只选择前5首歌作为大家都在听
    const popularSongs = [...songs].slice(0, 5);

    // 随机选择一些歌曲作为我的喜欢
    const likedSongs = shuffled.slice(3, 8);
    // 设置我的喜欢封面
    if (likedSongs.length > 0) {
      likedSongs[0].cover = 'https://img.js.design/assets/img/62c8f65c43eecc00bbccfa40.webp';
    }

    this.setData({
      guessSongs,
      popularSongs,
      likedSongs
    });
  },

  // 点击我的喜欢
  onMyLikesTap() {
    wx.navigateTo({
      url: '/pages/playlist/playlist?type=liked'
    });
  },

  // 点击猜你喜欢更多
  onGuessMoreTap() {
    wx.navigateTo({
      url: '/pages/guess/guess'
    });
  },

  // 点击歌单
  onPlaylistTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/playlist/playlist?id=${id}`
    });
  },

  // 点击歌曲
  onSongTap(e) {
    const songId = e.currentTarget.dataset.songId;
    const song = songs.find(s => s.id === songId);
    if (song) {
      const app = getApp();
      const playerManager = app.globalData.playerManager;
      if (playerManager) {
        // 设置当前歌曲
        playerManager.currentSong = song;
        // 播放音频
        playerManager.audioManager.src = song.src;
        playerManager.audioManager.title = song.title;
        playerManager.audioManager.singer = song.artist;
        playerManager.audioManager.coverImgUrl = song.cover;
        
        playerManager.audioManager.play();
        playerManager.isPlaying = true;
        
        // 跳转到播放页面
        wx.navigateTo({
          url: '/pages/player/player'
        });
      }
    }
  }
});